package business.global.pk.tdk;


import com.ddm.server.common.utils.CommTime;
import com.ddm.server.websocket.handler.requset.WebSocketRequestDelegate;

import cenum.PrizeType;
import core.server.ServerConfig;

import java.util.List;

/**
 * 玩家自动打牌
 * @author Administrator
 *
 */
public class TDKSetPosRobot {
	// 本局信息
	private TDKRoomSet set = null;
	private TDKSetRound setRound = null;
	public TDKSetPosRobot(TDKSetRound setRound) {
		this.setRound = setRound;
		this.set = this.setRound.getSet();
	}

	public void RobothandCrad(int posID) {
		// 获取等待操作的位置
		if (this.setRound.getWaitOpPos() != posID) {
			// 检查超时等待时间
			this.checkWaitTime(300);
			return;
		}
		// 获取回合操作者位置
		TDKRoundPos roundPos = this.setRound.roundPos(posID);
		if (null == roundPos) {
			// 检查超时等待时间
			this.checkWaitTime(10);
			return;
		}
		// 获取动作列表
		List<TDKdefine.TDK_OP> opList = roundPos.getRecieveOpTypes();
		if (null == opList || opList.size() <= 0) {
			// 检查超时等待时间
			this.checkWaitTime(10);
			return;
		}
		// 检查超时等待时间
		//YSJVB 30秒去掉
		//this.checkWaitTime(30);
		int value = this.set.getCurChip();
		TDKdefine.TDK_OP op = TDKdefine.TDK_OP.GenZhu;
		int maxPointPos = this.set.getMaxPointPos();
		TDKSetPos setPos = this.set.posDict.get(maxPointPos);
		if(null == setPos) {
			return;
		}
		if (setPos.getPid() > ServerConfig.getInitialID()) {
			// 玩家
			op = opList.get(this.set.getSetCard().nextInt(opList.size()));
		} else {
			// 机器人
			if (opList.contains(TDKdefine.TDK_OP.QiPai)) {
				op = TDKdefine.TDK_OP.QiPai;
			}else if (opList.contains(TDKdefine.TDK_OP.BuTi)) {
				op = TDKdefine.TDK_OP.BuTi;
			}else if (opList.contains(TDKdefine.TDK_OP.YaZhu)) {
				op = TDKdefine.TDK_OP.YaZhu;
			} else if (opList.contains(TDKdefine.TDK_OP.GenZhu)) {
				op = TDKdefine.TDK_OP.GenZhu;
			} else if (opList.contains(TDKdefine.TDK_OP.KaiPai)) {
				op = TDKdefine.TDK_OP.KaiPai;
			} else {
				op = opList.get(this.set.getSetCard().nextInt(opList.size()));
			}
		}

		if (TDKdefine.TDK_OP.YaZhu.equals(op)) {
			value = TDKdefine.TDK_DIFEN_TYPE.getFirstChipSet(this.set.getRoom().getDiFen(),this.set.getLanguoBeishu());
		}
		this.setRound.opCard(new WebSocketRequestDelegate(), posID, op,value);
	}

	/**
	 * 检查超时等待时间
	 */
	private void checkWaitTime (int minW) {
		if (PrizeType.Gold.equals(set.getRoom().getBaseRoomConfigure().getPrizeType())) {
			if (CommTime.nowSecond() - this.setRound.getStartTime() >= minW) {
				this.set.endSet();
			}
		}
	}
}
